# This file is part of the Lumenn project 
# at http://code.google.com/p/lumenn/
#
# Released under the GPLv3 at http://www.gnu.org/copyleft/gpl.html
# Designed for pygtk and the Maemo platform.
#
# Copyright 2008 by Jackson Yee (jackson@gotpossum.com)

import os
import os.path
import re
import random

from lumenn.utils import *
from lumenn.log import log

# *********************************************************************
def MkDir(dirname):
	ps	=	dirname.split(os.sep)
	
	if ps[0] == '':
		ps[0] = '/'
	
	for i in range(1, len(ps) + 1 ):
		try:
			os.mkdir( os.path.join( * ps[0:i] ) )
		except EnvironmentError, e:
			if e.errno != 17:
				log.write(e)

# *********************************************************************
class FileInfo(object):
	
	# -------------------------------------------------------------------
	def __init__(self, 
			dirname		=	None, 
			filename	=	None, 
			size			=	None, 
			modtime		=	None,
		):
		self.dirname	=	dirname
		self.filename	=	filename
		self.size			=	size
		self.modtime	=	modtime
	
	# -------------------------------------------------------------------
	def GetPath(self):
		return os.path.join(self.dirname, self.filename)
		
	# -------------------------------------------------------------------
	def GetExtension(self):
		return os.path.splitext(self.filename)[1]
	
# *********************************************************************
class FileList(object):
	# -------------------------------------------------------------------
	def __init__(self, 
			sort = None, 
			dirname = None, 
			pattern = None, 
			exclude = None, 
			recurse = False,
		):
		self.ls = []
		self.randomls	=	[]
		self.sort = sort
		
		if dirname and pattern:
			self.Load(dirname, pattern, exclude, recurse)
		
	# -------------------------------------------------------------------
	def __len__(self):
		return len(self.ls)
		
	# -------------------------------------------------------------------
	def __getitem__(self, pos):
		return self.ls[pos]
		
	# -------------------------------------------------------------------
	def __delitem__(self, k):
		del self.ls[k]
		
	# -------------------------------------------------------------------
	def Load(self, 
			dirname, 
			pattern = None, 
			exclude = None, 
			recurse = True, 
			callback = None,
		):
		if pattern:
			pattern = re.compile(pattern, re.IGNORECASE)

		if exclude:
			exclude = re.compile(exclude, re.IGNORECASE)
		
		self.LoadRecurse(dirname, pattern, exclude, recurse, callback)
		return 
	
	# -------------------------------------------------------------------
	def LoadRecurse(self, dirname, pattern, exclude, recurse, callback):
		for f in os.listdir(dirname):
			fullpath = os.path.join( dirname, f)
			
			if os.path.isdir( fullpath ):
				if recurse:
					
					if exclude:
						if exclude.match( fullpath ):
							continue
					
					self.LoadRecurse( fullpath, pattern, exclude, True, callback)
			else:
				if pattern:
					if not pattern.match(f):
						continue
				
				if exclude:
					if exclude.match(f):
						continue
				
				st = os.stat( fullpath )
				info	=	FileInfo(dirname, f, st.st_size, st.st_mtime)
				
				self.ls.append( info )
				
				if callback:
					callback(self, info)
		
	# -------------------------------------------------------------------
	def Sort(self):
		if not self.sort:
			return
		
		order = []
		
		for s in self.sort:
			if 'dirname' in s:
				if 'desc' in s:
					order.append( (0, 0) )
				else:
					order.append( (0, 1) )
			elif 'filename' in s:
				if 'desc' in s:
					order.append( (1, 0) )
				else:
					order.append( (1, 1) )
			elif 'size' in s:
				if 'desc' in s:
					order.append( (2, 0) )
				else:
					order.append( (2, 1) )
			elif 'mtime' in s:
				if 'desc' in s:
					order.append( (3, 0) )
				else:
					order.append( (3, 1) )
		
		self.ls.sort( ListCmp( order ) )
		
	# -------------------------------------------------------------------
	def Random(self):
		random.shuffle(self.ls)
		
	# -------------------------------------------------------------------
	def Clear(self):
		self.ls = []
